package com.wa.config;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wa.handlerMethodArgumentResolver.UserArgumentResolver;
import com.wa.interceptor.AccessLimitInterceptor;
import org.springframework.boot.validation.MessageInterpolatorFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.*;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * MVC配置类
 */
@Configuration
//引入了这样一个类DelegatingWebMvcConfiguration，通过@Bean注册了和<mvc:annotation-driven/>一样的组件，
public class WebConfig extends WebMvcConfigurationSupport {

    @Resource
    private UserArgumentResolver userArgumentResolver;
    @Resource
    private AccessLimitInterceptor accessLimitInterceptor;

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(userArgumentResolver);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(accessLimitInterceptor)
                 //拦截地址
                .addPathPatterns("/**")
                //排除地址
                .excludePathPatterns(this.getExcludePathPatternsArray());
    }

    private String[] getExcludePathPatternsArray() {
        return new String[]{"/handlerMethodArgumentResolver/user"};
    }



//    @Bean
//    public HttpMessageConverter<String> responseBodyConverter() {
//        StringHttpMessageConverter converter = new StringHttpMessageConverter(StandardCharsets.UTF_8);
//        converter.setWriteAcceptCharset(false);
//        return converter;
//    }
//
//    @Override
//    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//        converters.add(responseBodyConverter());
//
//        ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().build();
//        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//        converters.add(new MappingJackson2HttpMessageConverter(objectMapper));
//
//        // 解决BeanUtils.copyProperties 转换对象当有Date属性为空报错问题(org.apache.commons.beanutils.ConversionException: No value specified for 'Date')
////        ConvertUtils.register(new DateConverter(null), java.util.Date.class);
//    }
//
//    @Override
//    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
//        configurer.favorPathExtension(false);
//    }
//
//    /**
//     * 参数校验器
//     *
//     * @return
//     */
//    @Override
//    protected Validator getValidator() {
//        LocalValidatorFactoryBean factoryBean = new LocalValidatorFactoryBean();
//        MessageInterpolatorFactory interpolatorFactory = new MessageInterpolatorFactory();
//        factoryBean.setMessageInterpolator(interpolatorFactory.getObject());
//        // 快速失败（遇到校验失败，马上终止后面的校验）
//        factoryBean.getValidationPropertyMap().put("hibernate.validator.fail_fast", "true");
//
//        return factoryBean;
//    }
}